package tagdb.model;

import tagdb.algorithm.match.PeptideSpectrumMatch;

import java.util.*;
import java.util.Map.Entry;

public class ProteinFamilyHit {
    private String name;
    private String description;
    private TreeMap<String, ProteinHit> proteinHits = new TreeMap<String, ProteinHit>();
    private int uniqueSpectralCount = 0;

    public ProteinFamilyHit(String name, String description, ProteinHit proteinhit) {
        this.name = name;
        this.description = description;
        addProteinHit(proteinhit);

    }

    /**
     * Adds one peptide to the protein hit.
     *
     * @param proteinhit The protein hit
     */
    public void addProteinHit(ProteinHit proteinhit) {
        proteinHits.put(proteinhit.getAccession(), proteinhit);
    }

    /**
     * Returns the name of the family.
     *
     * @return the name of the family
     */
    public String getName() {
        return name;
    }

    /**
     * Returns the family description.
     *
     * @return the family description
     */
    public String getDescription() {
        return description;
    }


    /**
     * Returns the proteinhits of the family.
     *
     * @return the proteinhits of the family
     */
    public TreeMap<String, ProteinHit> getProteinHits() {
        return proteinHits;
    }

    public void setProteinHits(TreeMap<String, ProteinHit> proteinHits) {
        this.proteinHits = proteinHits;
    }

    public int getUniqueSpectralCount() {
        if (uniqueSpectralCount == 0) calculateUniqueSpectralCount();
        return uniqueSpectralCount;
    }

    private void calculateUniqueSpectralCount() {
        Set<String> spectraSet = new HashSet<String>();
        Set<Entry<String, ProteinHit>> entrySet = proteinHits.entrySet();
        for (Entry<String, ProteinHit> entry : entrySet) {
            List<PeptideHit> peptideHitList = entry.getValue().getPeptideHitList();
            for (PeptideHit peptideHit : peptideHitList) {
                List<PeptideSpectrumMatch> psms = new ArrayList<PeptideSpectrumMatch>(peptideHit.getSpectrumMatches().values());
                for (PeptideSpectrumMatch psm : psms) {
                    spectraSet.add(psm.getSpectrumTitle());
                }
            }
        }
        uniqueSpectralCount = spectraSet.size();
    }
}
